<template>
  <div class="app-container">
    <el-table v-loading="tableLoading" :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="sbpNum" label="收缩压基准线"> </el-table-column>
      <el-table-column prop="sbpLevel1" label="收缩压偏高提醒(0%<偏高<=n%)"> </el-table-column>
      <el-table-column prop="sbpLevel2" label="收缩压中度提醒(偏高%<中度<=n%)"> </el-table-column>
      <el-table-column prop="sbpLevel3" label="收缩压严重提醒(> 中度%)"> </el-table-column>
      <el-table-column prop="dbpNum" label="舒张压基准线"> </el-table-column>
      <el-table-column prop="dbpLevel1" label="舒张压偏高提醒(0%<偏高<=n%)"> </el-table-column>
      <el-table-column prop="dbpLevel2" label="舒张压中度提醒(偏高%<中度<=n%)"> </el-table-column>
      <el-table-column prop="dbpLevel3" label="舒张压严重提醒(> 中度%)"> </el-table-column>

      <el-table-column label="操作" align="center" width="190px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="showDetails(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="item ? '修改' : '新增'" :visible.sync="openView" width="700px" append-to-body>
      <basics-form style="margin-left: 100px" ref="basicsAddForm" :titleAligin="'right'" :titleWidth="'240px'"
          :actionLoading="loading" hiddenMustIcon @actionBtnsClick="addAction" @submit="addSubmit" />
    </el-dialog>
  </div>
</template>

<script>
  import BasicsForm from "@/components/BasicsForm";
  import {
    getBloodStandard,
  saveBloodStandard
  } from "@/api/manbing/hospital";
  export default {
    name: "Hospital",
    components: {
      BasicsForm
    },
    data() {
      return {
        loading: false,
        tableLoading: false,
        openView: false, //新增医院弹出
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        tableData: [],
        provinces: null, //省市区列表
        levels: null, //医院等级列表
        item: null, //操作的医院
      };
    },
    created() {
      this.$nextTick(() => {
        this.getList();
      });
    },
    methods: {
      //列表数据
      getList() {
        this.loading = true;
        this.tableLoading = true;
        getBloodStandard({})
          .then((res) => {
            if (res.code == 200) {
              this.tableData = res.data;
            }
          })
          .finally(() => {
            this.loading = false;
            this.tableLoading = false;
          });
      },
      //详情和编辑
      showDetails(item) {
        this.openView = true;
        this.item = item;
        this.$nextTick(() => {
          this.initAddRules();
        });
      },
      //构建新增/修改表单
      initAddRules() {
        let item = this.item ? this.item : {};
        let rules = [
          {
            title: "收缩压基准线",
            key: "sbpNum",
            value: item.sbpNum || "",
            ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
            ruleMsgList: ['收缩压基准线必须是纯数字无小数', '收缩压基准线最小为1', '收缩压基准线最大为1000']
          },
          {
            title: "收缩压偏高提醒(0%<偏高<=n%)",
            key: "sbpLevel1",
            value: item.sbpLevel1 || "",
            ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
            ruleMsgList: ['收缩压偏高提醒必须是纯数字无小数', '收缩压偏高提醒最小为1', '收缩压偏高提醒最大为1000']
          },
          {
            title: "收缩压中度提醒(偏高%<中度<=n%)",
            key: "sbpLevel2",
            value: item.sbpLevel2 || "",
            ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
            ruleMsgList: ['收缩压中度提醒必须是纯数字无小数', '收缩压中度提醒最小为1', '收缩压中度提醒最大为1000'],
          },
          {
            title: "收缩压严重提醒：> 中度%",
            type:'title',
            hiddenBottom:true,
            ruleWidth: "503px",
            // value: item.sbpLevel3 || "",
            // ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
            // ruleMsgList: ['收缩压严重提醒必须是纯数字无小数', '收缩压严重提醒最小为1', '收缩压严重提醒最大为1000'],
          },
          {
              title: "舒张压基准线",
              key: "dbpNum",
              value: item.dbpNum || "",
              type: 'number',
              ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
              ruleMsgList: ['舒张压基准线必须是纯数字无小数', '舒张压基准线最小为1', '舒张压基准线最大为1000']
            },
            {
              title: "舒张压偏高提醒(0%<偏高<=n%)",
              key: "dbpLevel1",
              value: item.dbpLevel1 || "",
              ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
              ruleMsgList: ['舒张压偏高提醒必须是纯数字无小数', '舒张压偏高提醒最小为1', '舒张压偏高提醒最大为1000'],
            },
            {
              title: "舒张压中度提醒(偏高%<中度<=n%)",
              key: "dbpLevel2",
              value: item.dbpLevel2 || "",
              ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
              ruleMsgList: ['舒张压中度提醒必须是纯数字无小数', '舒张压中度提醒最小为1', '舒张压中度提醒最大为1000'],
            },
            {
              title: "舒张压严重提醒：> 中度%",
              type:'title',
              hiddenBottom:true,
              ruleWidth:'503px',
              // title: "舒张压严重提醒(%)",
              // key: "dbpLevel3",
              // value: item.dbpLevel3 || "",
              // ruleList: ['isNum', 'numMin:1', 'numMax:1000'],
              // ruleMsgList: ['舒张压严重提醒必须是纯数字无小数', '舒张压严重提醒最小为1', '舒张压严重提醒最大为1000'],
            },
          {
            type: "actionBtns",
            selectList: [{
                name: "取消",
                value: ""
              },
              {
                name: "提交",
                value: "primary"
              },
            ],
            btnsAligin: "center",
            ruleWidth: "503px",
          },
        ];
        this.$refs.basicsAddForm.initForm(rules);
      },
      //自定义表单校验
      customValid(data) {
        let errorTip = "";
         if (
           data.sbpLevel2 <=data.sbpLevel1
        ) {
          errorTip = "收缩压中度提醒必须大于收缩压偏高提醒";
        }else if (data.dbpLevel2 <=data.dbpLevel1
        ) {
          errorTip = "舒张压中度提醒必须大于舒张压偏高提醒";
        }
        if (errorTip) {
         this.$modal.msgWarning(errorTip);
          return false
        } else {
         return true
        }
      },
      //新增表单的按钮点击事件
      addAction(index) {
        if (index) {
          this.$refs.basicsAddForm.formValid();
        } else {
          this.openView = false;
        }
      },
      //新增，发送请求
      addSubmit(param) {
        console.log(param,'sssssss');
        if (!param) {
          return;
        }
        let flag= this.customValid(param)
        if(!flag){
          return
        }
        this.loading = true;
        for (let key in param) {
          this.item[key] = param[key]
        }
        saveBloodStandard(this.item)
          .then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("修改成功");
              this.openView = false;
              this.getList();
            }
          })
          .finally(() => {
            this.loading = false;
          });
      },
    },
  };
</script>
<style scoped lang="scss">
</style>
